package models

import (
	"TheTearAfter/mysql"
	"TheTearAfter/util"
	"strconv"
	"strings"
	"sync"

	"github.com/jinzhu/gorm"
)

/**
 * @File : messageBlock.go
 * @Description : 信息块模型
 * @Author : chen
 * @Date : 2024/4/7
 */
type MessageBlock struct {
	gorm.Model
	Link  string `json:"link"`  // 信息链接
	Img   string `json:"img"`   // 信息图像
	Time  string `json:"time"`  // 信息时间
	Title string `json:"title"` // 信息标题
	From  string `json:"from"`  // 信息来源
}

func NewMessageBlock() (item MessageBlock) {
	item.Link = "https://www.bilibili.com/"
	item.Img = "https://s21.ax1x.com/2024/04/10/pFXP2i4.png"
	item.Time = "11 April 2019"
	item.Title = "The journey of the king"
	item.From = "Beastly forefathers"
	return
}

func (MB *MessageBlock) GetTimeList() (timeList [3]int, err error) {
	strList := strings.Split(MB.Time, " ")
	strList[1] = strList[1][0 : len(strList[1]) - 1]

	timeList[0], err = strconv.Atoi(strList[2])
	if err != nil {
		return
	}
	timeList[1] = util.MonthStrToNum(strList[0])
	timeList[2], _ = strconv.Atoi(strList[1])
	return
}

// 创建
func CreateAMessageBlock(item *MessageBlock) (*MessageBlock, error) {
	err := mysql.DB.Create(&item).Error
	return item, err
}

// 获取所有
func GetAllMessageBlock() (itemList []*MessageBlock, err error) {
	// itemList = []*MessageBlock{}
	err = mysql.DB.Find(&itemList).Error
	return
}

// 通过 id 获取
func GetMessageBlockByID(id uint) (item *MessageBlock, err error) {
	item = &MessageBlock{}
	err = mysql.DB.Where("id = ?", id).First(&item).Error
	return
}

// 更新
func UpdataAMessageBlock(item *MessageBlock) (err error) {
	err = mysql.DB.Save(&item).Error
	return
}

// 通过 id 删除
func DeleteMessageBlockByID(id uint, deep bool) (err error) {
	linkList, err := GetALLMessageTypeClass_Link_MessageBlockByMessageBlock(id)
	if err != nil {
		return
	}

	for _, link := range linkList {
		err = DeleteMessageTypeClass_Link_MessageBlockByID(link.ID, true)
		if err != nil {
			return
		}
	}

	if deep {
		// 硬删除
		err = mysql.DB.Unscoped().Where("id = ?", id).Delete(MessageBlock{}).Error
		return
	}

	err = mysql.DB.Where("id = ?", id).Delete(MessageBlock{}).Error
	return
}

// 信息 存储
type MessageBlockSyncList struct {
	mutex sync.Mutex
	list  []*MessageBlock
}

// 添加 信息
func (mes *MessageBlockSyncList) Add(messageList []*MessageBlock) {
	mes.mutex.Lock()
	defer mes.mutex.Unlock()
	mes.list = append(mes.list, messageList...)
}

// 获取长度
func (mes *MessageBlockSyncList) GetLen() (l int) {
	l = len(mes.list)
	return
}

// 获取列表
func (mes *MessageBlockSyncList) GetList() []*MessageBlock {
	return mes.list
}

// 数组排序定义
type MessageBlockList []*MessageBlock

func (MBL MessageBlockList) Len() int {
	return len(MBL)
}

func (MBL MessageBlockList) Swap(i int, j int) {
	MBL[i], MBL[j] = MBL[j], MBL[i]
}

func (MBL MessageBlockList) Less(i int, j int) (bool) {
	timeI, err := MBL[i].GetTimeList()
	if err != nil {
		util.ErrPrintRespon("(MBL MessageBlockList) Less", err)
		return false
	}
	timeJ, err := MBL[j].GetTimeList()
	if err != nil {
		util.ErrPrintRespon("(MBL MessageBlockList) Less", err)
		return false
	}

	for i := 0; i < len(timeI); i++ {
		if(timeI[i] == timeJ[i]) {
			continue
		}
		return timeI[i] > timeJ[i]
	}

	return false
}